package com.yixing.tech.wms.cross;

import com.yixing.tech.common.base.BaseResponse;
import com.yixing.tech.common.base.BusinessException;
import com.yixing.tech.common.entity.wcs.TaskVO;
import com.yixing.tech.wcs.service.IBoxTaskService;
import com.yixing.tech.wcs.service.ITaskService;
import com.yixing.tech.wms.feigh.WcsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * WcsService
 *
 * @author <a href="mailto:brucezhang_jjz@163.com">zhangj</a>
 * @since
 */
@Slf4j
@Service
public class CrossWcsServiceImpl implements WcsService {

    @Lazy
    @Autowired
    private ITaskService taskService;
    @Lazy
    @Autowired
    private IBoxTaskService boxTaskService;

    @Override
    public BaseResponse<Object> genTask(TaskVO taskVO) throws BusinessException {
        log.info("genTask=======================================");
        log.info("请求参数:{}", taskVO);
        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(1000);
        response.setDesc("下发任务成功!");
        response.setData(taskService.genTask(taskVO));
        return response;
    }

    @Override
    public BaseResponse<Object> cancelTask(TaskVO taskVO) throws BusinessException {
        log.info("cancelTask=======================================");
        log.info("请求参数:{}", taskVO);
        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(1000);
        response.setDesc("取消任务成功!");
        response.setData(taskService.cancelTask(taskVO));
        return response;
    }

    @Override
    public BaseResponse<Object> updateBoxState(Map<String, Object> map) throws BusinessException {

        if(ObjectUtils.isEmpty(map) || ObjectUtils.isEmpty(map.get("podCode"))) {
            throw new BusinessException("podCode参数不能为空!");
        }
        if(ObjectUtils.isEmpty(map.get("interactionState"))) {
            throw new BusinessException("interactionState参数不能为空!");
        }

        log.info("请求更新料箱交互状态:podCode[{}],state[{}]", map.get("podCode").toString(), map.get("interactionState").toString());
        boxTaskService.updateBoxState(map.get("podCode").toString(), map.get("interactionState").toString());
        BaseResponse<Object> response = new BaseResponse<>();
        response.setCode(1000);
        response.setDesc("更新状态成功!");
        response.setData(null);
        return response;
    }
}
